package com.beamfield.controller.manage.steelplant;

import com.alibaba.druid.util.StringUtils;
import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.SteelplantWeightDiameter;
import com.beamfield.service.manage.steelplant.SteelplantWeightSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/cms/steelplant/setting")
public class WeightSettingController extends BaseController {
    private final SteelplantWeightSettingService steelplantDiameterWeightService;

    @Autowired
    public WeightSettingController(SteelplantWeightSettingService steelplantDiameterWeightService) {
        this.steelplantDiameterWeightService = steelplantDiameterWeightService;
    }

    @RequestMapping("/unitWeightDiameter")
    public ModelAndView index(){
        return new ModelAndView();
    }


    @RequestMapping("/getRebarModel")
    @ResponseBody
    public ApiResult getRebarModel(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        return new ApiResult(steelplantDiameterWeightService.getRebarModel(beamId));
    }

    /**
     * 保存钢筋的直径和重量信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveWeight",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request, SteelplantWeightDiameter steelplantWeightDiameter){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if(StringUtils.isEmpty(steelplantWeightDiameter.getDiameter())||StringUtils.isEmpty(steelplantWeightDiameter.getWeight().toString())){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        steelplantWeightDiameter.setAddtime(new Date());
//        steelplantWeightDiameter.setBeam_id(beamId);
        steelplantWeightDiameter.setOperator(userId);
        int result = 0;
        try{
            result = steelplantDiameterWeightService.save(steelplantWeightDiameter);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"保存成功 ");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),"已存在此直径设置");
    }
    /**
     * 删除钢筋信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/delWeight",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doDelete(HttpServletRequest request, String idId){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        int result = 0;
        try{
            result = steelplantDiameterWeightService.delete(idId ,beamId ,userId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"删除成功");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
    }
    /**
     * 查询钢筋重量和直径信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readWeight",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readProcDadoGroup(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
//        Long beamId = super.getBeamId(request);
        Long beamId = null;
        List<SteelplantWeightDiameter> steelplantWeightDiameterList = steelplantDiameterWeightService.getSteelplantWeightDiameter(beamId,offset,limit);
        long total = steelplantDiameterWeightService.countByBeamId(beamId);
        /*for(SteelplantWeightDiameter steelplantWeightDiameter:steelplantWeightDiameterList){
            System.out.println(steelplantWeightDiameter);
        }*/
        return new PageResult(total, Long.parseLong(offset), steelplantWeightDiameterList);

    }

    /**
     * 修改钢筋的直径和重量信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/editWeight",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doEdit(HttpServletRequest request, SteelplantWeightDiameter steelplantWeightDiameter){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        if(StringUtils.isEmpty(steelplantWeightDiameter.getDiameter())||StringUtils.isEmpty(steelplantWeightDiameter.getWeight().toString())){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        steelplantWeightDiameter.setAddtime(new Date());
//        steelplantWeightDiameter.setBeam_id(beamId);
        steelplantWeightDiameter.setOperator(userId);
        int result = 0;
        try{
            result = steelplantDiameterWeightService.updateByPrimaryKey(steelplantWeightDiameter);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        if(result == 1){
            return new ApiResult(HttpResultEnum.SUCCESS.getCode(),"保存成功 ");
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
    }



}
